﻿Imports System.Xml
Imports Framework 'Provides some simple xml helper methods (code is open-source at: picasso.codeplex.com)

Module Module1

    Sub Main()
        Try

            Dim targetPath As String = "C:\dev\MikeExport\DataFrom_Polaris\" ' With Trailing slash \
            Dim source As String = "http://192.168.1.2:8080/" ' With Trailing slash /

            Dim wc As New Net.WebClient
            wc.Credentials = New Net.NetworkCredential("Polaris", "") 'Configured in Polaris


            RecurseFolder(wc, source, targetPath)

            Console.ReadLine()


        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Console.ReadLine()

    End Sub
    Private Sub RecurseFolder(wc As Net.WebClient, source As String, targetPath As String)
        If Not IO.Directory.Exists(targetPath) Then IO.Directory.CreateDirectory(targetPath)
        Dim cacheFile As String = targetPath & "root.xml"

        Dim s As String = Nothing
        If IO.File.Exists(cacheFile) Then
            s = IO.File.ReadAllText(cacheFile)
        Else
            s = DownloadString(wc, source)
            IO.File.WriteAllText(cacheFile, s)
        End If


        Dim doc As New XmlDocument()
        doc.LoadXml(s)



        Dim entries As XmlNode = CXml.ChildNode(doc, "entries")
        Dim entryList As List(Of XmlNode) = CXml.ChildNodes(entries, "entry")

        For Each entry As XmlNode In entryList
            Dim kind As String = CXml.ChildNodeGetStr(entry, "kind")
            Dim name As String = CXml.ChildNodeGetStr(entry, "name")
            Dim path As String = CXml.ChildNodeGetStr(entry, "path")
            If kind = "devicepath" Then
                Console.WriteLine("Skipped: " & name)
                Continue For
            End If
            If kind = "file" Then
                Dim filePath As String = targetPath & name
                Dim fileUrl As String = (source & "\" & name).Replace("/\", "/")
                DownloadFile(wc, fileUrl, filePath)
                Continue For
            End If
            If name = ".." Then Continue For
            'Recursion
            RecurseFolder(wc, source & name & "/", (targetPath & name & "\"))
        Next
    End Sub

    Private Function DownloadString(ByRef wc As Net.WebClient, url As String) As String
        Try
            Return wc.DownloadString(url)
        Catch ex As Exception
            Console.WriteLine("Failed to download url: " & url)
            Threading.Thread.Sleep(3000)
            Try
                Return wc.DownloadString(url)
            Catch ex2 As Exception
                Console.WriteLine("Failed to download url, spawning new webclient")
                wc.Dispose()

                wc = New Net.WebClient
                wc.Credentials = New Net.NetworkCredential("Polaris", "")
                Return DownloadString(wc, url)
            End Try
        End Try
    End Function
    Private Sub DownloadFile(ByRef wc As Net.WebClient, url As String, path As String)
        Try
            wc.DownloadFile(url, path)
        Catch ex As Exception
            Console.WriteLine("Failed to download file: " & url)
            Threading.Thread.Sleep(30000)
            Try
                wc.DownloadFile(url, path)
            Catch ex2 As Exception
                Console.WriteLine("Failed to download file, spawning new webclient")
                wc.Dispose()

                wc = New Net.WebClient
                wc.Credentials = New Net.NetworkCredential("Polaris", "")
                DownloadFile(wc, url, path)
            End Try
        End Try
    End Sub


End Module
